package kodkod.ast.visitor.graph;

import java.io.*;
import java.util.*;

import kodkod.analysis.AST.GraphVisitor;
import kodkod.ast.*;

/*
 * Traverses the AST and outputs a Graph in CSV edge-list format
 */

public class GraphVisitorCSV extends GraphVisitor {
	
	File graphFile = null;
	BufferedWriter writer = null;
	
	public GraphVisitorCSV() {
		try {
			graphFile = File.createTempFile("Graph", ".csv");
			writer = new BufferedWriter(new FileWriter(graphFile));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void finalize() {
		try {
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void flush() {
		try {
			writer.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@Override
	protected void newEdge(Node parent, Node child) {
		try {
			writer.write(getNodeName(parent)+";"+getNodeName(child));
			writer.newLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void newNode(Node node) {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void enter(Node node) {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void leave(Node node) {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void newCompound(Node parent, Node child) {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void newEdge(Node parent, Node child, int pos) {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected boolean visited(Node n) {
		// TODO Auto-generated method stub
		return false;
	}
}
